package kr.hkit.androidcomplete03.ch25;

import kr.hkit.androidcomplete03.R;
import kr.hkit.androidcomplete03.ch25.LocalService.LocalBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

public class BindServiceTest extends Activity {
	 LocalService mService; 
	    boolean mBound = false; 
	 
	    protected void onCreate(Bundle savedInstanceState) { 
	        super.onCreate(savedInstanceState); 
	        setContentView(R.layout.bindservicetest); 
	    } 
	 
	    protected void onStart() { 
	        super.onStart(); 
	        Intent intent = new Intent(this, LocalService.class); 
	        bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
	    } 
	 
	    protected void onStop() { 
	        super.onStop(); 
	        if (mBound) { 
	            unbindService(mConnection); 
	            mBound = false; 
	        } 
	    } 
	    
	    public void onButtonClick(View v) { 
	        if (mBound) { 
	            int num = mService.getRandomNumber(); 
	            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show(); 
	        } 
	    } 
	 
	    private ServiceConnection mConnection = new ServiceConnection() { 
	        public void onServiceConnected(ComponentName className, IBinder service) { 
	            LocalBinder binder = (LocalBinder) service; 
	            mService = binder.getService(); 
	            mBound = true; 
	        } 
	 
	        public void onServiceDisconnected(ComponentName arg0) { 
	            mBound = false; 
	        } 
	    };
}
